package com.situ.mall.common.service.impl;

import com.situ.mall.common.entity.Member;
import com.situ.mall.common.entity.UserBaseInfo;
import com.situ.mall.common.entity.Worker;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.MemberMapper;
import com.situ.mall.common.mapper.WorkerMapper;
import com.situ.mall.common.service.ILoginService;
import com.situ.mall.common.util.JWTUtil;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Random;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private WorkerMapper workerMapper;

    /* 随机头像地址的数组 */
    static String[] avatarList = {
            "https://tdesign.gtimg.com/mobile/demos/avatar1.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar2.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar3.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar4.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar5.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar1.png",
    };

    /* 随机背景地址的数组 */
    static String[] backgroundPicList = {
            "https://tdesign.gtimg.com/mobile/demos/avatar1.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar2.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar3.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar4.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar5.png",
            "https://tdesign.gtimg.com/mobile/demos/avatar1.png",
    };

    /* 随机给定头像，昵称，背景图片 */
    public static UserBaseInfo setUserInfo(String avatar, String nickname) {
        UserBaseInfo u = new UserBaseInfo();
        //随机给定头像
        if (StringUtil.isBlank(avatar)) {
            //随机到的头像的下标
            int index = new Random().nextInt(avatarList.length);
            //给头像赋随机值
            avatar = avatarList[index];
        }
        //随机给定昵称
        if (StringUtil.isBlank(nickname)) {
            nickname = StringUtil.getString(6);
        }
        //随机给定背景
        String backgroundPic = backgroundPicList[new Random().nextInt(backgroundPicList.length)];

        u.setAvatar(avatar);
        u.setNickname(nickname);
        u.setBackgroundPic(backgroundPic);

        return u;
    }

    /* 前端请求 */
    //登陆验证
    @Override
    public ResultVO check(String tel, String password, String type) {
        Object o;
        Member m = new Member();
        Worker w = new Worker();

        //1.判断手机号格式
        StringUtil.checkTel(tel);

        //2.根据用户类型，去不同的表里查用户
        if (Objects.equals(type, "1")) {
            o = memberMapper.selectByTel(tel);
            m = (Member) o;
        } else {
            o = workerMapper.selectByTel(tel);
            w = (Worker) o;
        }

        //3.查不到，说明手机号都不对
        if (o == null) {
            throw new MyException("手机号错误");
        }

        //4.再根据用户类型比对密码
        if (Objects.equals(type, "1")) {
            if (m.getPassword() == null) {
                throw new MyException("请重置密码");
            } else if (m.getPassword().equals(StringUtil.md5(password))) {
                // **生成token
                String token = JWTUtil.createToken(m.getNickname(), m);
                m.setToken(token);
                // 把token存入数据库
                memberMapper.insertToken(token, m.getId());
                return ResultVO.data(200, "登陆成功", m);
            } else {
                throw new MyException("密码错误");
            }
        } else {
            if (w.getPassword() == null) {
                throw new MyException("请重置密码");
            } else if (w.getPassword().equals(StringUtil.md5(password))) {
                // **生成token
                String token = JWTUtil.createToken(w.getNickname(), w);
                w.setToken(token);
                // 把token存入数据库
                workerMapper.insertToken(token, w.getId());
                return ResultVO.data(200, "登陆成功", w);
            } else {
                throw new MyException("密码错误");
            }
        }
    }

    //注册保存
    @Override
    public ResultVO register(String avatar, String nickname, String tel, String password, String type, String name) {
        Object o;

        //1.判断手机号格式
        StringUtil.checkTel(tel);

        //2.看手机号有没有被注册
        if (Objects.equals(type, "1")) {
            o = memberMapper.selectByTel(tel);
        } else {
            o = workerMapper.selectByTel(tel);
        }
        //能查到，说明手机号已被注册，报错
        if (o != null) {
            throw new MyException("手机号已被注册！");
        } else {

            //3.如果没被注册，继续
            //随机一个头像，昵称，和背景图
            UserBaseInfo info = LoginServiceImpl.setUserInfo(avatar, nickname);
            if (Objects.equals(type, "1")) {
                Member m = new Member();
                m.setAvatar(info.getAvatar());
                m.setBackgroundPic(info.getBackgroundPic());
                m.setNickname(info.getNickname());
                m.setStatus("0");
                m.setTel(tel);
                //给密码加密
                m.setPassword(StringUtil.md5(password));
                //保存到数据库
                memberMapper.register(m);
                //再查一遍
                Member currentUser = memberMapper.selectByTel(m.getTel());
                return ResultVO.data(200, "注册成功", currentUser);
            } else {
                Worker w = new Worker();
                w.setAvatar(info.getAvatar());
                w.setBackgroundPic(info.getBackgroundPic());
                w.setNickname(info.getNickname());
                w.setStatus("0");
                w.setName(name);
                w.setTel(tel);
                //给密码加密
                w.setPassword(StringUtil.md5(password));
                //保存到数据库
                workerMapper.register(w);
                //再查一遍
                Worker currentUser = workerMapper.selectByTel(w.getTel());
                return ResultVO.data(200, "注册成功", currentUser);
            }
        }
    }

}
